package handle

import (
	"protocx/protobufx"

	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/context"
)

type Protobuf struct {
}

func (r Protobuf) InitRoute(party iris.Party) {
	p := party.Party("/protobuf")
	p.Post("/readProtobuf", r.ReadProtobuf())
	p.Post("/protobuf", r.Protobuf())
	p.Post("/readJsonProtobuf", r.ReadJSONProtobuf())
}
func (r Protobuf) Protobuf() iris.Handler {
	return func(context *context.Context) {
		context.Protobuf(&protobufx.LoginRequest{
			Code: 879,
		})
	}
}

func (r Protobuf) ReadProtobuf() iris.Handler {
	return func(ctx *context.Context) {
		var request protobufx.LoginRequest

		err := ctx.ReadProtobuf(&request)
		if err != nil {
			ctx.StopWithError(iris.StatusBadRequest, err)
			return
		}

		ctx.JSON(&request)
	}
}
func (r Protobuf) ReadJSONProtobuf() iris.Handler {
	return func(ctx *context.Context) {
		var request protobufx.LoginRequest

		err := ctx.ReadJSONProtobuf(&request)
		if err != nil {
			ctx.StopWithError(iris.StatusBadRequest, err)
			return
		}

		ctx.JSON(&request)
	}
}
